# 批量插入数据
from data_define import Order
from file_define import JsonFileReader, TextFileReader
from pymysql import Connection

if __name__ == '__main__':
    # 从文件中读取订单集合
    text_file_reader = TextFileReader("../cap13/data/2011年1月销售数据.txt")
    json_file_reader = JsonFileReader("../cap13/data/2011年2月销售数据JSON.txt")

    jan_data: list[Order] = text_file_reader.read_data()
    feb_data: list[Order] = json_file_reader.read_data()
    all_data = jan_data + feb_data

    # 链接数据库并插入数据
    conn = Connection(
        host="192.168.0.88",
        port=3308,
        user="root",
        password="root"
    )

    cursor = conn.cursor()
    """
    create table tb_order(
        order_id varchar(64) not null,
        order_date date not null,
        money int not null,
        province varchar(64) not null,
        primary key (order_id)
    )engine=innodb default charset=utf8mb4;
    """
    conn.select_db("hmdp")
    for order in all_data:
        cursor.execute(
            f"insert into tb_order (order_id,order_date,money,province) values ('{order.order_id}','{order.date}',{order.money},'{order.province}')")

    conn.commit()
    conn.close()

    print("执行完成")
